<template>
  <div style="padding: 10px 15px">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-select
        v-model="form.payChannel"
        clearable
        filterable
        class="filter-item"
        placeholder="请选择支付通道"
      >
        <el-option
          v-for="item in paytychanneloptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span v-html="item.label" />
        </el-option>
      </el-select>
      <el-button
        class="filter-item seachbttn"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
      <el-button
        class="filter-item seachbttn"
        type="primary"
        @click="handleFilter(2)"
      >
        清空
      </el-button>
    </div>
    <div
      class="filter_fenge"
      style="border: none; border-top: 1px solid #d1ddeb"
    ></div>
    <div class="filter-container">
      <el-button
        v-if="Jurisdiction('添加')"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
        >新增</el-button
      >
      <el-button
        v-if="Jurisdiction('删除')"
        class="filter-item bttnleft"
        type="danger"
        icon="el-icon-delete"
        @click="deleteClick()"
        >删除</el-button
      >
      <el-button
        class="filter-item bttnleft"
        type="primary"
        icon="el-icon-download"
        @click="exportFormVisible = true"
        >导出</el-button
      >
    </div>
    <!--查询条件 end-->

    <!--table-->
    <el-table
      v-loading="listLoading"
      :data="RcncbtchData"
      fit
      highlight-current-row
      style="width: 100%"
      stripe
    >
      <el-table-column
        label="对账日期"
        align="right"
        width="100px"
        min-width="150px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.dtStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="left" min-width="200px">
        <template slot-scope="{ row }">
          <span>{{ row.paychannel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付金额/元" align="right" min-width="100px">
        <template slot-scope="{ row }">
          <span>{{ row.payAmtStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付笔数" align="right" min-width="100px">
        <template slot-scope="{ row }">
          <span>{{ row.numPaymentStr }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />

    <!--table end -->

    <!--弹出层  新增-->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="55%"
    >
      <el-form :model="form" :inline="true" label-position="right">
        <el-form-item label="主键编号">
          <el-input v-model="form.id" />
        </el-form-item>
        <el-form-item label="对账日期">
          <el-input v-model="form.dt" />
        </el-form-item>
        <el-form-item label="企业">
          <el-input v-model="form.cpnID" />
        </el-form-item>
        <el-form-item label="机构">
          <el-input v-model="form.orgID" />
        </el-form-item>
        <el-form-item label="支付方式">
          <el-input v-model="form.payTyp" />
        </el-form-item>
        <el-form-item label="支付渠道">
          <el-input v-model="form.payChannel" />
        </el-form-item>
        <el-form-item label="支付账户">
          <el-input v-model="form.act" />
        </el-form-item>
        <el-form-item label="西联支付实收金额">
          <el-input v-model="form.xLAmtMony" />
        </el-form-item>
        <el-form-item label="西联支付平台实收笔数">
          <el-input v-model="form.xLAmtCount" />
        </el-form-item>
        <el-form-item label="第三方平台实收金额">
          <el-input v-model="form.tHAmtMony" />
        </el-form-item>
        <el-form-item label="第三方平台实收笔数">
          <el-input v-model="form.tHAmtCount" />
        </el-form-item>
        <el-form-item label="西联支付平台退款金额">
          <el-input v-model="form.xLAmtMonyRfd" />
        </el-form-item>
        <el-form-item label="西联支付平台退款笔数">
          <el-input v-model="form.xLAmtCountRfd" />
        </el-form-item>
        <el-form-item label="第三方平台退款金额">
          <el-input v-model="form.tHAmtMonyRfd" />
        </el-form-item>
        <el-form-item label="第三方平台退款笔数">
          <el-input v-model="form.tHAmtCountRfd" />
        </el-form-item>
        <el-form-item label="对账方式">
          <el-input v-model="form.typ" />
        </el-form-item>
        <el-form-item label="处理状态">
          <el-input v-model="form.dealStt" />
        </el-form-item>
        <el-form-item label>
          <el-input v-model="form.rcncbtchTyp" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导出" :visible.sync="exportFormVisible" width="20%">
      <el-form :model="downloadform" :inline="true" label-position="right">
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.nowexportLoading"
          @click="Export(false)"
          >按当前页导出</el-button
        >
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.allexportLoading"
          @click="Export(true)"
          >全部导出</el-button
        >
      </el-form>
    </el-dialog>

    <!--详情-->
    <el-dialog title="详情" :visible.sync="dialogdetailsVisible" width="55%">
      <el-form :model="details_form" :inline="true" label-position="right" />
    </el-dialog>

    <!--弹出层 end-->
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { AjaxToken, TipConfirm } from "@/api/common";
import { SelectAjax } from "@/api/common";
import { Jurisdiction } from "@/api/common";
import sessionStorage from "@/api/Storage";
import commconfig from "@/comm.json";
import "url-search-params-polyfill";
export default {
  name: "Channel",
  components: { Pagination },
  data() {
    return {
      logincpnid: "",
      exportFormVisible: false,
      sy: "system",
      user: null,
      multipleSelection: [],
      formLabelWidth: "120px",
      RcncbtchData: null,
      listQuery: {
        title: "",
        total: 0,
        page: 1,
        limit: 10,
      },
      listLoading: true,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      cpnoptions: [],
      paytypeoptions: [],
      paytychanneloptions: [],
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "添加",
      },
      form: {
        id: "",
        dt: "",
        btchId: "",
        cpnID: "",
        orgID: "",
        payTyp: "",
        payChannel: "",
        act: "",
        xLAmtMony: "",
        xLAmtCount: "",
        tHAmtMony: "",
        tHAmtCount: "",
        xLAmtMonyRfd: "",
        xLAmtCountRfd: "",
        tHAmtMonyRfd: "",
        tHAmtCountRfd: "",
        typ: "",
        eRPAmtMony: "",
        eRPAmtCount: "",
        eRPAmtMonyRfd: "",
        eRPAmtCountRfd: "",
        dealStt: "",
        rcncbtchTyp: "",
      },
      downloadform: {
        allexportLoading: false,
        nowexportLoading: false,
      },
      details_form: {
        id: "",
        dt: "",
        btchId: "",
        cpnID: "",
        orgID: "",
        payTyp: "",
        payChannel: "",
        act: "",
        xLAmtMony: "",
        xLAmtCount: "",
        tHAmtMony: "",
        tHAmtCount: "",
        xLAmtMonyRfd: "",
        xLAmtCountRfd: "",
        tHAmtMonyRfd: "",
        tHAmtCountRfd: "",
        typ: "",
        eRPAmtMony: "",
        eRPAmtCount: "",
        eRPAmtMonyRfd: "",
        eRPAmtCountRfd: "",
        dealStt: "",
        rcncbtchTyp: "",
      },
    };
  },
  mounted() {},
  // 初始化
  created() {
    var user = JSON.parse(sessionStorage.getItem("userinfo"));
    this.user = user;
    this.logincpnid = user.cpnId;
    this.fetchData();
    backstage.GeCpntListItems(this);
    backstage.GetPayTypeList(this);
    backstage.GetPyChannelSelectListItems(this);
  },
  methods: {
    // 权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    // 搜索
    handleFilter(typ) {
      if (typ == 2) {
        this.listQuery.page = 1;
        this.listQuery.title = "";
        this.form = {
          payChannel: "",
        };
        this.fetchData();
      } else {
        this.listQuery.page = 1;
        this.fetchData();
      }
    },
    // 新增
    handleCreate() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.form = {
        id: "",
        dt: "",
        btchId: "",
        cpnID: "",
        orgID: "",
        payTyp: "",
        payChannel: "",
        act: "",
        xLAmtMony: "",
        xLAmtCount: "",
        tHAmtMony: "",
        tHAmtCount: "",
        xLAmtMonyRfd: "",
        xLAmtCountRfd: "",
        tHAmtMonyRfd: "",
        tHAmtCountRfd: "",
        typ: "",
        eRPAmtMony: "",
        eRPAmtCount: "",
        eRPAmtMonyRfd: "",
        eRPAmtCountRfd: "",
        dealStt: "",
        rcncbtchTyp: "",
      };
    },
    // 编辑
    handleClick(row) {
      this.form.id = row.id;
      this.form.dt = row.dt;
      this.form.btchId = row.btchId;
      this.form.cpnID = row.cpnID;
      this.form.orgID = row.orgID;
      this.form.payTyp = row.payTyp;
      this.form.payChannel = row.payChannel;
      this.form.act = row.act;
      this.form.xLAmtMony = row.xLAmtMony;
      this.form.xLAmtCount = row.xLAmtCount;
      this.form.tHAmtMony = row.tHAmtMony;
      this.form.tHAmtCount = row.tHAmtCount;
      this.form.xLAmtMonyRfd = row.xLAmtMonyRfd;
      this.form.xLAmtCountRfd = row.xLAmtCountRfd;
      this.form.tHAmtMonyRfd = row.tHAmtMonyRfd;
      this.form.tHAmtCountRfd = row.tHAmtCountRfd;
      this.form.typ = row.typ;
      this.form.eRPAmtMony = row.eRPAmtMony;
      this.form.eRPAmtCount = row.eRPAmtCount;
      this.form.eRPAmtMonyRfd = row.eRPAmtMonyRfd;
      this.form.eRPAmtCountRfd = row.eRPAmtCountRfd;
      this.form.dealStt = row.dealStt;
      this.form.rcncbtchTyp = row.rcncbtchTyp;
      this.dialogStatus = "edit";
      this.dialogFormVisible = true;
    },
    // 保存
    saveForm() {
      var arr = this.multipleSelection;
      var params = new URLSearchParams();
      params.append("Id", this.form.id);
      params.append("Dt", this.form.dt);
      params.append("BtchId", this.form.btchId);
      params.append("CpnID", this.form.cpnID);
      params.append("OrgID", this.form.orgID);
      params.append("PayTyp", this.form.payTyp);
      params.append("PayChannel", this.form.payChannel);
      params.append("Act", this.form.act);
      params.append("XLAmtMony", this.form.xLAmtMony);
      params.append("XLAmtCount", this.form.xLAmtCount);
      params.append("THAmtMony", this.form.tHAmtMony);
      params.append("THAmtCount", this.form.tHAmtCount);
      params.append("XLAmtMonyRfd", this.form.xLAmtMonyRfd);
      params.append("XLAmtCountRfd", this.form.xLAmtCountRfd);
      params.append("THAmtMonyRfd", this.form.tHAmtMonyRfd);
      params.append("THAmtCountRfd", this.form.tHAmtCountRfd);
      params.append("Typ", this.form.typ);
      params.append("ERPAmtMony", this.form.eRPAmtMony);
      params.append("ERPAmtCount", this.form.eRPAmtCount);
      params.append("ERPAmtMonyRfd", this.form.eRPAmtMonyRfd);
      params.append("ERPAmtCountRfd", this.form.eRPAmtCountRfd);
      params.append("DealStt", this.form.dealStt);
      params.append("RcncbtchTyp", this.form.rcncbtchTyp);
      backstage.SaveRcncbtch(this, params);
      this.fetchData();
    },
    // 删除
    deleteClick(row) {
      var multis = [];
      var params = new URLSearchParams();
      if (row == undefined || row == "") {
        var arr = this.multipleSelection;
        if (arr == undefined || arr.length <= 0) {
          this.$message({
            showClose: true,
            message: "请至少选中一行",
            type: "error",
          });
          return;
        } else {
          for (var i = 0; i < arr.length; i++) {
            multis.push(arr[i].id);
          }
          params.append("delds", JSON.stringify(multis));
        }
      } else {
        multis.push(row.id);
        params.append("delds", JSON.stringify(multis));
      }
      this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then((a) => {
          this.listLoading = true;
          backstage.DelRcncbtch(this, params);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 查询
    fetchData() {
      this.listLoading = true;
      var params = new URLSearchParams();
      params.append(
        "Parms",
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          "}"
      );

      params.append("Title", this.listQuery.title);
      params.append(
        "CpnID",
        this.form.cpnID == "" ? this.user.cpnId : this.form.cpnID
      );
      params.append("PyTypID", this.form.payTyp);
      params.append("PyChnlID", this.form.payChannel);
      params.append(
        "Tp",
        this.$route.query.tp == undefined ? "" : this.$route.query.tp
      );
      backstage.GetRcncbtchLst(this, params);
    },
    //跳转差异详情
    OpenDifOrds(row) {
      this.$router.push({
        path: "/Reconciliation/DifOrds",
        query: { btchid: row.btchId },
      });
    },
    //订单处理记录
    OpenRcnHandRcd(row) {
      this.$router.push({
        path: "/Reconciliation/RcnHandRcd",
        query: { btchid: row.btchId },
      });
    },
    Export(Export) {
      if (Export) {
        this.downloadform.allexportLoading = true;
      } else {
        this.downloadform.nowexportLoading = true;
      }
      backstage.ChannelExport(this, Export);
    },
  },
};
// 后台事件
var backstage = {
  ChannelExport: function (thisobj, Export) {
    var cpnid =
      thisobj.form.cpnID == "" ? thisobj.user.cpnId : thisobj.form.cpnID;
    var tp =
      thisobj.$route.query.tp == undefined ? "" : thisobj.$route.query.tp;
    var url =
      commconfig.baseapi +
      "ChannelExport?Parms=" +
      '{"page": "' +
      thisobj.listQuery.page +
      '","rows": "' +
      thisobj.listQuery.limit +
      '"}' +
      "&Export=" +
      Export +
      "&Title=" +
      thisobj.listQuery.title +
      "&PyTypID=" +
      thisobj.form.payTyp +
      "&Tp=" +
      tp +
      "&PyChnlID=" +
      thisobj.form.payChannel +
      "&cpnid=" +
      cpnid;
    if (Export) {
      thisobj.downloadform.allexportLoading = false;
    } else {
      thisobj.downloadform.nowexportLoading = false;
    }
    window.location.href = url;
  },
  // 获取Rcncbtch集合
  GetRcncbtchLst: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetChannel_Lst",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data.data;
        thisobj.RcncbtchData = body.rows;
        thisobj.listQuery.page = body.page;
        thisobj.listQuery.total = body.records;
      },
      function (err) {}
    );
  },
  SaveRcncbtch: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "SaveRcncbtch",
      params,
      "post",
      function (res) {
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 3000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
  DelRcncbtch: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "DelRcncbtch",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 3000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
  GeCpntListItems: function (thisobj) {
    SelectAjax(
      "GeCpntListItems?val=" + thisobj.cpnid + "",
      function (response) {
        var body = response.data;
        thisobj.cpnoptions = [];
        body.data.forEach((element) => {
          thisobj.cpnoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetPayTypeList: function (thisobj) {
    SelectAjax(
      "GetPayTypeList?val=",
      function (response) {
        var body = response.data;
        thisobj.paytypeoptions = [];
        body.data.forEach((element) => {
          thisobj.paytypeoptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
  GetPyChannelSelectListItems: function (thisobj) {
    SelectAjax(
      "GetPyChannelSelectListItems?val=",
      function (response) {
        var body = response.data;
        thisobj.paytychanneloptions = [];
        body.data.forEach((element) => {
          thisobj.paytychanneloptions.push({
            value: element.value,
            label: element.text,
          });
        });
      },
      function (error) {}
    );
  },
};
</script>
